package com.superman.dao.impl;

import com.superman.dao.ICarDao;
import com.superman.entity.Car;
import com.superman.entity.Car_Brand_Category;
import com.superman.util.JDBCTemplate;
import com.superman.util.ResultSetHandler;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class CarDaoImpl implements ICarDao {
    /**
     * 返回指定条件的汽车对象
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    private List<Car_Brand_Category> queryCar(String sql, Object[] params) throws SQLException, ClassNotFoundException {

        return JDBCTemplate.query(sql, params, new ResultSetHandler<Car_Brand_Category>(){
            @Override
            public Car_Brand_Category handleRow(ResultSet rs) throws SQLException {
                return new Car_Brand_Category(
                        rs.getString("id"), rs.getString("car_number"), rs.getString("brand_id"),
                        rs.getString("model"), rs.getString("color"), rs.getString("category_id"),
                        rs.getString("t_comments"), rs.getDouble("price"), rs.getDouble("rent"),
                        rs.getInt("status"), rs.getInt("useable"), rs.getString("brand"),
                        rs.getString("category")
                );
            }
        });
    }

    /**
     * 返回所有汽车对象
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    @Override
    public List<Car_Brand_Category> queryCarAll() throws SQLException, ClassNotFoundException {
        String sql = "SELECT * FROM t_car_brand_category_view";
        Object[] params = null;
        return queryCar(sql, params);

    }



    /**
     * 查找并返回指定汽车编号的 可租用 已上架 的汽车对象
     * @param car_id 指定汽车编号
     * @return 用户选择的可租用汽车对象
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    @Override
    public List<Car_Brand_Category> queryCarByCarID(String car_id) throws SQLException, ClassNotFoundException {
        String sql = "SELECT * FROM t_car_brand_category_view WHERE id = ? ";
        Object[] params = {car_id};
        return queryCar(sql, params);

    }

    @Override
    public List<Car_Brand_Category> queryCarByCarIdNumber(String car_number) throws SQLException, ClassNotFoundException {
        String sql = "SELECT * FROM t_car_brand_category_view WHERE car_number = ? AND status = 0 AND useable = 0";
        Object[] params = {car_number};
        return queryCar(sql, params);
    }

    /**
     * 当车辆被租用时，修改t_car 表中的汽车状态为不可租用
     * @param car_id 选择租用的汽车对象id
     * @param conn 当前租车操作的连接
     * @return 更新的行数
     * @throws SQLException
     */
    @Override
    public int userUpdateCarStatus(String car_id, int status, Connection conn) throws SQLException {
        String sql = "UPDATE t_car SET status = ? WHERE ID = ?";
        Object[] params = {status, car_id};
        return JDBCTemplate.update(sql, params, conn);
    }

    /**
     * 添加一条汽车记录
     * @param car 要添加的汽车对象
     * @return 添加的行数
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    @Override
    public int insertCar(Car car) throws SQLException, ClassNotFoundException {
        String sql = "INSERT INTO t_car VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        Object[] params = {car.getId(), car.getCar_number(), car.getBrand_id(), car.getModel(), car.getColor(),
                car.getCategory_id(), car.getT_comments(), car.getPrice(), car.getRent(), car.getStatus(),
                car.getUseable()};
        return JDBCTemplate.update(sql, params);
    }

    /**
     * 修改指定汽车的租赁价格
     * @param car_id 指定汽车id
     * @param rent 修改的租赁价格
     * @return 修改的行数
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    @Override
    public int updateCarRent(String car_id, double rent) throws SQLException, ClassNotFoundException {
        String sql = "UPDATE t_car SET rent = ? WHERE id = ?";
        Object[] params = {rent, car_id};
        return JDBCTemplate.update(sql, params);
    }

    /**
     * 修改指定汽车的上架状态
     * @param car_id
     * @param useable
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    @Override
    public int updateCarUseable(String car_id, int useable) throws SQLException, ClassNotFoundException {
        String sql = "UPDATE t_car SET useable = ? WHERE id = ?";
        Object[] params = {useable, car_id};
        return JDBCTemplate.update(sql, params);
    }


}
